import Vue from "vue";
import VueRouter from "vue-router";
import layout from "../views/layout/index.vue";

Vue.use(VueRouter);

// 默认路由
export const defaultRouter = [
  {
    path: "/",
    redirect: "login",
    hidden: true,
    meta: {
      name: "主页",
      icon: ""
    }
  },
  {
    path: "/login",
    name: "login",
    hidden: true,
    mata: {
      name: "登录",
      icon: ""
    },
    component: () => import("../views/login/index.vue")
  },
  /**
   * 控制台
   */
  {
    path: "/layout",
    name: "Layout",
    redirect: "/index",
    meta: {
      name: "控制台",
      icon: "console"
    },
    component: layout,
    children: [
      {
        path: "/index",
        name: "Index",
        meta: {
          name: "首页",
          icon: ""
        },
        component: () => import("../views/console/index")
      }
    ]
  }
];

export default new VueRouter({
  // 滚动行为 https://router.vuejs.org/zh/guide/advanced/scroll-behavior.html
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { x: 0, y: 0 };
    }
  },
  routes: defaultRouter
});

// 动态路由
export const asyncRouter = [
  /**
   * 信息管理
   */
  {
    path: "/info",
    name: "Info",
    meta: {
      role: ["sale"],
      system: "infoSystem",
      name: "信息管理",
      icon: "info"
    },
    component: layout,
    children: [
      {
        path: "/infoindex",
        name: "InfoIndex",
        meta: {
          role: ["sale"],
          keepAlive: true,
          name: "信息列表",
          icon: ""
        },
        component: () => import("../views/info/index")
      },
      {
        path: "/infocategory",
        name: "InfoCategory",
        meta: {
          role: ["sale"],
          keepAlive: true,
          name: "信息分类",
          icon: ""
        },
        component: () => import("../views/info/category")
      },
      {
        path: "/infoeditdetails",
        name: "InfoEditDetails",
        hidden: true,
        meta: {
          role: ["sale"],
          keepAlive: true,
          name: "编辑详情",
          icon: ""
        },
        component: () => import("../views/info/editdetails")
      }
    ]
  },
  /**
   * 用户管理
   */
  {
    path: "/user",
    name: "User",
    meta: {
      role: ["sale"],
      system: "userSystem",
      name: "用户管理",
      icon: "user"
    },
    component: layout,
    children: [
      {
        path: "/userindex",
        name: "UserIndex",
        meta: {
          role: ["sale"],
          keepAlive: true,
          name: "用户列表"
        },
        component: () => import("../views/user/index")
      }
    ]
  },
  /**
   * 404
   */
  {
    path: "/404",
    name: "error_404",
    hidden: true,
    component: () => import("../views/errorPage/404")
  },
  {
    path: "/401",
    name: "error_401",
    hidden: true,
    component: () => import("../views/errorPage/401")
  },
  // 放置路由最下面 "*" 匹配所有
  { path: "*", redirect: "/404", hidden: true }
];
